package bll

import (
	"fmt"

	"7qule.com/sd3/CrossServer/src/bll/player/bll"
	"7qule.com/sd3/CrossServer/src/common/onlinelog"
	"7qule.com/sd3/CrossServer/src/common/systemConstant"
	"public.com/goutil/syncUtil"
)

type globalKuangzhanGuild struct {
	// 帮派领地的信息
	data map[string]*gangTerritory

	// 数据同步对象
	dataLock *syncUtil.RWLocker
}

var GlobalKuangzhanGuild = &globalKuangzhanGuild{
	dataLock: syncUtil.NewRWLocker(),
}

//
// InitData
// @Description: 初始化数据
// @receiver this
//
func (this *globalKuangzhanGuild) InitData() {
	globalGuildMap := bll.GlobalGuildBll.GetData()
	tempMap := make(map[string]*gangTerritory)
	for guildId := range globalGuildMap {
		temp := NewGangTerritory()
		for layerKey, layer := range GlobalKuangzhanBll.GetData() {
			for _, position := range layer.GetData() {
				if position.GuildId == guildId {
					temp.AddPlayerCount(layerKey)
				}
			}
		}
		tempMap[guildId] = temp
	}

	this.data = tempMap
	return
}

//
// GetGangTerritory
// @Description: 获取帮派领地
// @receiver this
// @param guildId 帮派id
// @return result 帮派领地
// @return exist 帮派是否存在
//
func (this *globalKuangzhanGuild) GetGangTerritory(guildId string) (result map[int32]int32, exist bool) {
	if isOk, prevStack, currStack := this.dataLock.RLock(systemConstant.DefaultLockTime); isOk == false {
		errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s  \n当前堆栈:\n%s", prevStack, currStack)
		onlinelog.ErrorLog(errMsg)
		panic(errMsg)
	}
	defer this.dataLock.RUnlock()

	gangTerritory, exist := this.data[guildId]
	if exist {
		result = gangTerritory.GetData()
		return
	}
	return
}

//
// AddGangTerritoryPlayerCount
// @Description: 增加帮派领地人数
// @receiver this
// @param guildId 帮派id
// @param layerId 层数id
//
func (this *globalKuangzhanGuild) AddGangTerritoryPlayerCount(guildId string, layerId int32) {
	if isOk, prevStack, currStack := this.dataLock.Lock(systemConstant.DefaultLockTime); isOk == false {
		errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s  \n当前堆栈:\n%s", prevStack, currStack)
		onlinelog.ErrorLog(errMsg)
		panic(errMsg)
	}
	defer this.dataLock.Unlock()

	gangTerritory, exist := this.data[guildId]
	if !exist {
		gangTerritory = NewGangTerritory()
		this.data[guildId] = gangTerritory
	}
	gangTerritory.AddPlayerCount(layerId)
}

//
// ReduceGangTerritoryPlayerCount
// @Description: 减少帮派领地人数
// @receiver this
// @param guildId 帮派id
// @param layerId 层数id
//
func (this *globalKuangzhanGuild) ReduceGangTerritoryPlayerCount(guildId string, layerId int32) {
	if isOk, prevStack, currStack := this.dataLock.Lock(systemConstant.DefaultLockTime); isOk == false {
		errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s  \n当前堆栈:\n%s", prevStack, currStack)
		onlinelog.ErrorLog(errMsg)
		panic(errMsg)
	}
	defer this.dataLock.Unlock()

	gangTerritory, exist := this.data[guildId]
	if !exist {
		gangTerritory = NewGangTerritory()
		this.data[guildId] = gangTerritory
	}
	gangTerritory.ReducePlayerCount(layerId)
}

//
// AddGuild
// @Description: 添加工会
// @receiver this
// @param guildId 工会id
// @return *gangTerritory 工会领地
//
func (this *globalKuangzhanGuild) AddGuild(guildId string) {
	if isOk, prevStack, currStack := this.dataLock.Lock(systemConstant.DefaultLockTime); isOk == false {
		errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s  \n当前堆栈:\n%s", prevStack, currStack)
		onlinelog.ErrorLog(errMsg)
		panic(errMsg)
	}
	defer this.dataLock.Unlock()

	this.data[guildId] = NewGangTerritory()
}

//
// DeleteGuild
// @Description: 删除工会
// @receiver this
// @param guildId 工会id
//
func (this *globalKuangzhanGuild) DeleteGuild(guildId string) {
	if isOk, prevStack, currStack := this.dataLock.Lock(systemConstant.DefaultLockTime); isOk == false {
		errMsg := fmt.Sprintf("Lock timeout! \n上一个堆栈:\n%s  \n当前堆栈:\n%s", prevStack, currStack)
		onlinelog.ErrorLog(errMsg)
		panic(errMsg)
	}
	defer this.dataLock.Unlock()

	delete(this.data, guildId)
}
